/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newBook.java
 *
 * Created on Sep 16, 2010, 2:07:11 PM
 */
package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author DeKNOR
 */
public class BOOK_ADD extends javax.swing.JFrame {

     Connection con;
     Statement stat;

    /** Creates new form newBook */
    public BOOK_ADD() throws SQLException {
        super("ADD BOOK");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTextField2 = new javax.swing.JTextField();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        bookCostTextField = new javax.swing.JTextField();
        volumeLabel = new javax.swing.JLabel();
        ISBNLabel = new javax.swing.JLabel();
        publisherTextField = new javax.swing.JTextField();
        bookTitleTextField = new javax.swing.JTextField();
        bookCostLabel = new javax.swing.JLabel();
        bookTitleLabel = new javax.swing.JLabel();
        genreComboBox = new javax.swing.JComboBox();
        authorTextField = new javax.swing.JTextField();
        genreLabel = new javax.swing.JLabel();
        ISBNTextField = new javax.swing.JTextField();
        confirmButton = new javax.swing.JButton();
        publisherLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        volumeTextField = new javax.swing.JTextField();
        authorLabel = new javax.swing.JLabel();
        chargeLabel = new javax.swing.JLabel();
        chargeTextField = new javax.swing.JTextField();
        bookCodeLabel = new javax.swing.JLabel();
        bookcodeTextField = new javax.swing.JTextField();
        availablelabel = new javax.swing.JLabel();
        availableComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        bookCostTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookCostTextFieldActionPerformed(evt);
            }
        });
        bookCostTextField.setBounds(560, 170, 150, 30);
        jLayeredPane1.add(bookCostTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        volumeLabel.setText("Volume");
        volumeLabel.setBounds(450, 120, 90, 30);
        jLayeredPane1.add(volumeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ISBNLabel.setText("Isbn");
        ISBNLabel.setBounds(450, 270, 90, 30);
        jLayeredPane1.add(ISBNLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        publisherTextField.setBounds(160, 220, 210, 30);
        jLayeredPane1.add(publisherTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);
        bookTitleTextField.setBounds(160, 170, 213, 30);
        jLayeredPane1.add(bookTitleTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCostLabel.setText("Book Cost");
        bookCostLabel.setBounds(450, 170, 100, 30);
        jLayeredPane1.add(bookCostLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookTitleLabel.setText("Book Title");
        bookTitleLabel.setBounds(60, 170, 90, 30);
        jLayeredPane1.add(bookTitleLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genreComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SELECT", "COMEDY", "ANIME", "ADVENTURE", "ROMANTIC", "CRIME FICTION", "SUPERHERO" }));
        genreComboBox.setBounds(160, 320, 210, 30);
        jLayeredPane1.add(genreComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        authorTextField.setBounds(160, 270, 210, 30);
        jLayeredPane1.add(authorTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genreLabel.setText("Genre");
        genreLabel.setBounds(60, 320, 90, 30);
        jLayeredPane1.add(genreLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        ISBNTextField.setBounds(560, 270, 150, 30);
        jLayeredPane1.add(ISBNTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        confirmButton.setText("ADD");
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });
        confirmButton.setBounds(480, 390, 110, 40);
        jLayeredPane1.add(confirmButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        publisherLabel.setText("Publisher");
        publisherLabel.setBounds(60, 220, 80, 30);
        jLayeredPane1.add(publisherLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        cancelButton.setBounds(600, 390, 110, 40);
        jLayeredPane1.add(cancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        volumeTextField.setBounds(560, 120, 150, 30);
        jLayeredPane1.add(volumeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        authorLabel.setText("Author");
        authorLabel.setBounds(60, 270, 80, 30);
        jLayeredPane1.add(authorLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        chargeLabel.setText("Charge");
        chargeLabel.setBounds(450, 220, 100, 30);
        jLayeredPane1.add(chargeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        chargeTextField.setBounds(560, 220, 150, 30);
        jLayeredPane1.add(chargeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel.setText("Book Code");
        bookCodeLabel.setBounds(60, 120, 90, 30);
        jLayeredPane1.add(bookCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookcodeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookcodeTextFieldActionPerformed(evt);
            }
        });
        bookcodeTextField.setBounds(160, 120, 210, 30);
        jLayeredPane1.add(bookcodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        availablelabel.setText("Availablity");
        availablelabel.setBounds(450, 320, 90, 30);
        jLayeredPane1.add(availablelabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        availableComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SELECT", "YES", "NO" }));
        availableComboBox.setBounds(560, 320, 150, 30);
        jLayeredPane1.add(availableComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("ADD BOOK");
        jLabel1.setBounds(50, 40, 100, 30);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(50, 82, 740, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background5 copy.jpg"))); // NOI18N
        jLabel2.setBounds(-50, 0, 940, 500);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 892, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        
            new MAIN_MENU().setVisible(true);
        this.setVisible(false);
    }                                            

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
       String a = bookcodeTextField.getText();
       String b = bookTitleTextField.getText();
       String c  = bookCostTextField.getText();
       String d  = authorTextField.getText();
       String e  = (String) genreComboBox.getSelectedItem();
       String f  = ISBNTextField.getText();
       String g  = volumeTextField.getText();
       String h  = publisherTextField.getText();
       String i  = chargeTextField.getText();
       String j = (String) availableComboBox.getSelectedItem();
      


       if(a.equals("") || b.equals("") || c.equals("") || d.equals("") || f.equals("") || g.equals("") || h.equals("") || i.equals("") )
        {
            JOptionPane.showMessageDialog(null, "Type something to add first!");
        }

 else
        {
        String query = "INSERT INTO BOOK VALUES('"+ a +"','"+ b +"','"+ g +"',"+
                        "'"+ c +"','"+ d +"','"+e +"','"+h+"',"+
                        "'"+ i+"','"+j+"', '"+f+"')";

        String message = "Are you sure to add?";
        String title = "ADD BOOK";

        int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
        if(reply == JOptionPane.YES_OPTION)
        {
                try {
                    stat = con.createStatement();
                    stat.executeUpdate(query);

                     JOptionPane.showMessageDialog(null, "Add Successful");
                     
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_ADD.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "Add failed - some required fields are not filled, or bad input.");
                }

             clear();

            }

        }


    }                                             


    private  void clear()
    {
        bookcodeTextField.setText("");
       
        bookTitleTextField.setText("");
        bookCostTextField.setText("");
        authorTextField.setText("");
        genreComboBox.setSelectedIndex(0);
        ISBNTextField.setText("");
        volumeTextField.setText("");
        publisherTextField.setText("");
        chargeTextField.setText("");
        availableComboBox.setSelectedIndex(0);
    }





    private void bookcodeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
    }                                                 

    private void bookCostTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
    }                                                 

     private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");
    }

    /**
    * @param args the command line arguments
    */
     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BOOK_ADD().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_ADD.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel ISBNLabel;
    private javax.swing.JTextField ISBNTextField;
    private javax.swing.JLabel authorLabel;
    private javax.swing.JTextField authorTextField;
    private javax.swing.JComboBox availableComboBox;
    private javax.swing.JLabel availablelabel;
    private javax.swing.JLabel bookCodeLabel;
    private javax.swing.JLabel bookCostLabel;
    private javax.swing.JTextField bookCostTextField;
    private javax.swing.JLabel bookTitleLabel;
    private javax.swing.JTextField bookTitleTextField;
    private javax.swing.JTextField bookcodeTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel chargeLabel;
    private javax.swing.JTextField chargeTextField;
    private javax.swing.JButton confirmButton;
    private javax.swing.JComboBox genreComboBox;
    private javax.swing.JLabel genreLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel publisherLabel;
    private javax.swing.JTextField publisherTextField;
    private javax.swing.JLabel volumeLabel;
    private javax.swing.JTextField volumeTextField;
    // End of variables declaration                   

}
